package whu.edu.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import whu.edu.demo.dao.ProductDao;
import whu.edu.demo.domain.Product;
import whu.edu.demo.exception.ProductException;

import java.util.List;

@Service
public class ProductService extends ServiceImpl<ProductDao, Product> {
    @Autowired
    private ProductDao productDao;

    //加
    public  Product addProduct(Product p) throws ProductException {
        if(p.getId() != null){
            if(productDao.selectById(p.getId()) != null){
                throw new ProductException("商品已存在");
            }
        }
        productDao.insert(p);
        return p;
    }

    //删
    public void deleteProduct(int id){
        productDao.deleteById(id);
    }

    //改
    public void updateProduct(int id, Product p) throws ProductException {
        if(p.getId()!=null){
            if(productDao.selectById(id) == null){
                throw new ProductException("商品不存在");
            }
            p.setId(id);
            productDao.updateById(p);
        }
    }

    //查
    public Product getProduct(int id){
        return productDao.selectById(id);
    }
    //返回全部
    public List<Product> getAll(){
        return productDao.selectList(null);
    }

    //多条件查询
    public List<Product> getProduct(String name,Integer price){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(null != name,"name",name).lt(null != price,"price",price);
        return productDao.selectList(queryWrapper);
    }

    //多表查
    public List<ProductDao>findSupplier(int id){
        return productDao.findSupplier(id);
    }

}



